package com.ai.bitcoin.controller;

import com.ai.bitcoin.bo.HtmlReportGenerateBO;
import com.ai.bitcoin.bo.PushBO;
import com.ai.bitcoin.common.resp.ResponseEntity;
import com.ai.bitcoin.constant.CacheConstant;
import com.ai.bitcoin.constant.CommonConstant;
import com.ai.bitcoin.exception.BusinessException;
import com.ai.bitcoin.helper.JwtParseTokenHelper;
import com.ai.bitcoin.schedule.task.HtmlReportGenerateAllTask;
import com.ai.bitcoin.service.HtmlReportService;
import com.ai.bitcoin.util.DateUtil;
import com.ai.bitcoin.vo.HtmlReportGenerateVO;
import com.ai.bitcoin.weixin.push.WxPushHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * Created by gulj on 2018/6/11.
 */
@RestController
@RequestMapping("/html/report")
@CrossOrigin
@Slf4j
public class HtmlReportController extends CommonController {

    @Autowired
    private JwtParseTokenHelper jwtParseTokenHelper;

    @Autowired
    private HtmlReportService htmlReportService;

    @Autowired
    private WxPushHelper wxPushHelper;


    @Autowired
    private HtmlReportGenerateAllTask htmlReportGenerateAllTask;


    /**
     * 根据用户是否订阅，生成订阅和非订阅报告
     *
     * @param htmlReportGenerateBO
     * @return
     */
    @PostMapping("/generate")
    public ResponseEntity<HtmlReportGenerateVO> generate(@RequestBody HtmlReportGenerateBO htmlReportGenerateBO) {
        HtmlReportGenerateVO reportGenerate = null;
        try {
            long startTime = System.currentTimeMillis();
            log.info("用户订阅报告解析用户token开始：openId{}", htmlReportGenerateBO.getOpenId());
            // 解析用户token
            jwtParseTokenHelper.parseToken(htmlReportGenerateBO.getToken());
            log.info("用户订阅报告 解析用户token 结束：openId{},已耗时：{}s", htmlReportGenerateBO.getOpenId(), (System.currentTimeMillis() - startTime) / 1000.0);

            reportGenerate = htmlReportService.generate(htmlReportGenerateBO);
        } catch (BusinessException e) {
            return new ResponseEntity<>(400, e.getMessage(), null);
        }
        return new ResponseEntity<>(200, "success", reportGenerate);
    }


    /**
     * 判断用户是否已经订阅币
     *
     * @param htmlReportGenerateBO
     * @return
     */
    @PostMapping("/checkSubscribe")
    public ResponseEntity checkSubscribe(@RequestBody HtmlReportGenerateBO htmlReportGenerateBO) {
        boolean result = false;
        try {
            result = htmlReportService.checkSubscribe(htmlReportGenerateBO);
        } catch (BusinessException e) {
            return new ResponseEntity<>(500, e.getErrInfo(), null);
        }
        return new ResponseEntity<>(200, "success", result);
    }


    /***
     * 推送报告
     * @param pushBO
     * @return
     */
    @PostMapping("/pushWxReportMsg")
    public ResponseEntity pushWxReportMsg(@RequestBody PushBO pushBO) {
        wxPushHelper.pushReport(pushBO);
        return new ResponseEntity<>(200, "success", null);
    }


    @GetMapping("/batchGenerateReport")
    public ResponseEntity batchGenerateReport() {
        List<String> list = Arrays.asList("1026", "11", "12", "1202", "124", "1386", "1396", "141", "1449", "146", "19", "201", "235", "24", "27", "3", "31", "372", "374", "39", "4", "5", "50", "53", "57", "6", "655", "66", "7", "9", "90", "2", "10", "13", "14", "18", "17", "138", "22", "8");
        wxPushHelper.batchGenerateReport(list);
        return new ResponseEntity<>(200, "success", null);
    }

    @GetMapping("/crontab")
    public void crontab() {
        log.info("【生成报告触发定时任务开始】");
        htmlReportGenerateAllTask.execute();
        log.info("【生成报告触发定时任务结束】");
    }

    @GetMapping("/date")
    @ResponseBody
    public String date() {
        String key = CacheConstant.REPORT + DateUtil.currentDate2Str(CommonConstant.TIME_PATTERN);

        return key;
    }
}
